package com.caocaog.market.data.transfer;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @author liguang
 * @version 1.0.0
 * @Project jeecg-boot
 * @Package com.caocaog.market.data.transfer
 * @ClassName Test.java
 * @createTime 2021年11月16日 09:11:00
 * @Description TODO
 */
public class Test {
    /**
     * 通过地址位置信息，解析城市信息
     * @param location 地理信息，格式 经度,纬度
     *  114.05,22.55
     * @return
     */
    public static String parseLocation(String location){
        // https://lbs.amap.com/api/webservice/guide/api/georegeo 逆地址解析
        // amap_api 注册高德地图开发者，创建应用，获取apikey
        //测试key:8f21643950153e066e4bfefc3d244e19
        String key = "b8cc0845009644ee5e331702ca73249a";
        String url ="https://restapi.amap.com/v3/geocode/regeo?location="+location+"&key="+key+"&radius=1000&extensions=all";
        String jsonData =  HttpUtil.get(url);
        JSONObject jsonLocation = JSONUtil.parseObj(jsonData);
        String city = "";
        if("1".equals(jsonLocation.getStr("status"))){
            JSONObject addressComponent =jsonLocation.getJSONObject("regeocode").getJSONObject("addressComponent");
            Object obj = null;
            if ((obj = addressComponent.get("province")) instanceof String){
                // 如果是直辖市，通过province返回数据
                city= city + (String)obj;
            }
            if((obj = addressComponent.get("city")) instanceof String){
                city= city + (String)obj + addressComponent.get("adcode").toString();
            }
            if((obj = addressComponent.get("district")) instanceof String){
                city= city + (String)obj + addressComponent.get("adcode").toString();
            }

        }
        return city;
    }

    public static void main(String[] args) {
        System.out.println(parseLocation("109.223225,19.746837"));
    }
}
